//
//  RingminderAppDelegate.m
//  Ringminder
//
//  Created by Think Small Studio on 09-6-20.
//  Copyright Think Small Studio 2009. All rights reserved.
//

#import "RingminderAppDelegate.h"
#import "RootViewController.h"


@implementation RingminderAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize appData;
@synthesize pathToUserCopyOfPlist;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    // Override point for customization after app launch. Copy default appData.plist to Documents if not found.    
	NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    self.pathToUserCopyOfPlist = [documentsDirectory stringByAppendingPathComponent:@"appData.plist"];
    if ([fileManager fileExistsAtPath:pathToUserCopyOfPlist] == NO) {
        NSString *pathToDefaultPlist = [[NSBundle mainBundle] pathForResource:@"appData" ofType:@"plist"];
        if ([fileManager copyItemAtPath:pathToDefaultPlist toPath:pathToUserCopyOfPlist error:&error] == NO) {
            NSAssert1(0, @"Failed to copy data with error message '%@'.", [error localizedDescription]);
        }
    }
    // Unarchive the activeData, store it in the local property
    self.appData = [[[NSMutableArray alloc] initWithContentsOfFile:pathToUserCopyOfPlist] autorelease];
	
	[window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
	[appData writeToFile:pathToUserCopyOfPlist atomically:NO];
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[appData release];
	[pathToUserCopyOfPlist release];
	[navigationController release];
	[window release];
	[super dealloc];
}


@end

